<template>
  <view class="category_tab">
    <view class="category_tab_title">
      <view class="title_inner"
        ><uni-segmented-control
          :current="current"
          :values="items.map((v) => v.title)"
          @clickItem="onClickItem"
          styleType="text"
          activeColor="#d4237a"
        ></uni-segmented-control
      ></view>
      <view class="iconfont iconsearch"></view>
    </view>
    <scroll-view
      scroll-y
      enable-flex
      class="category_tab_content"
      @scrolltolower="handleToLower"
    >
      <view class="cate_item" v-for="(item, index) in vertical" :key="item.id">
        <go-detail :list="vertical" :index="index">
          <image mode="widthFix" :src="item.thumb"></image>
        </go-detail>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { uniSegmentedControl } from "@dcloudio/uni-ui";
import goDetail from "@/components/goDatil";

export default {
   components: {
    goDetail,
  },
  data() {
    return {
      current: 0,
      items: [
        { title: "最新", order: "new" },
        { title: "热门", order: "hot" },
      ],
      params: {
        limit: 30,
        skip: 0,
        order: "new",
      },
      id: 0,
      vertical: [],
      hasMore: true,
    };
  },
  onLoad(options) {
    this.id = options.id;
    this.getList();
  },
  methods: {
    onClickItem(res) {
      if (this.current != res.currentIndex) {
        this.current = res.currentIndex;
      } else {
        return;
      }
      this.params.order = this.items[res.currentIndex].order;
      this.params.skip = 0;
      this.vertical = [];
      this.getList();
    },
    getList() {
      this.request({
        url: `http://157.122.54.189:9088/image/v1/vertical/category/${this.id}/vertical`,
        data: this.params,
      }).then((res) => {
        if (res.res.vertical.length === 0) {
          this.hasMore = false;
          uni.showToast({
            title: "已是最后一页",
            icon: "none",
          });
          return;
        }
        this.vertical = [...this.vertical, ...res.res.vertical];
      });
    },
    handleToLower() {
      if (this.hasMore) {
        this.params.skip += this.params.limit;
        this.getList();
      } else {
        uni.showToast({
          title: "已是最后一页",
          icon: "none",
        });
      }
    },
  },
  components: {
    uniSegmentedControl,
  },
};
</script>

<style lang='scss'>
.category_tab {
  .category_tab_title {
    position: relative;
    .title_inner {
      width: 60%;
      margin: 0 auto;
    }
    .iconfont {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: 5%;
    }
  }
  .category_tab_content {
    height: calc(100vh - 36px);
    display: flex;
    flex-wrap: wrap;
    padding: 0 5rpx;
    .cate_item {
      width: 33.33%;
      border: 5rpx solid #fff;
    }
  }
}
</style>